import {useState,useEffect} from 'react'
import { message } from "antd";
import useDictionary from '@/hooks/useDictionary'
const useTable = (getFn:paginationListFn,config?:{[key:string]:any}) => {
    const [messageApi,contextHolder] = message.useMessage()
    const [columnList,setColumnList] = useState<any[]>([])
    const [current,setCurrentIndex_state] = useState<number>(1)
    const [pageSize,setPageSize_state] = useState<number>(10)
    const [total,setTotal] = useState<number>(0)
    const [data,setData] = useState<any[]>([])
    const [loading,setLoading] = useState<boolean>(false)
    const getTable = async() => {
         setLoading(true)
         try{
            let {datas,total:allNum,columns} = await getFn({page:current,pageSize,...config})
            setColumnList(columns)
            setTotal(allNum)
            setData(datas)
         }catch(error){
            message.error(useDictionary('RequestMessage'))
        }finally{
           setLoading(false)
        }
    }
    useEffect(()=>{
        getTable()
    },[current,pageSize])
    const setCurrent = async (pageNum:number) => {
         setCurrentIndex_state(pageNum)
    }
    const setPageSize = (current:number,pageSize:number) => {
        setPageSize_state(pageSize)
    }
    return {
        getTable,
        total,
        tableData:data,
        current,
        pageSize,
        setCurrent,
        setPageSize,
        loading,
        columnList,
        data
    }
}
export default useTable